package com.lyl.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyl.domain.SysRole;
import com.lyl.model.Result;
import com.lyl.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;

/**
 * @author lylbell
 * @date 2021-06-23 13:52
 */
@RestController
@Api(tags = "后台角色管理")
@RequestMapping("/roles")
public class SysRoleController {


    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 通过条件查询角色信息
     * @param page
     * @param name
     * @return
     */
    @GetMapping
    @ApiOperation(value="通过条件查询角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页大小"),
            @ApiImplicitParam(name = "name",value = "要查询的角色名")
    })
    @PreAuthorize("hasAuthority('sys_role_query')")
    public Result<Page<SysRole>> findByPage(@ApiIgnore Page<SysRole> page, String name) {
        page.addOrder(OrderItem.desc("last_update_time")); //添加排序能力
        Page<SysRole> rolePage = sysRoleService.findByPage(page,name);
        return Result.ok(rolePage);
    }


    @PostMapping
    @ApiOperation(value="添加角色")
    @ApiImplicitParams({
         @ApiImplicitParam(name = "sysRole",value = "角色的json数据")
    })
    @PreAuthorize("hasAuthority('sys_role_create')")
    public Result add(@RequestBody @Validated SysRole sysRole) {
        boolean save = sysRoleService.save(sysRole);
        if (save) {
            return Result.ok("角色添加成功");
        }
        return Result.fail("角色添加失败");
    }

    @PatchMapping
    @ApiOperation(value="修改角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysRole",value = "角色的json数据")
    })
    @PreAuthorize("hasAuthority('sys_role_update')")
    public Result update(@RequestBody @Validated SysRole sysRole) {
        boolean update = sysRoleService.updateById(sysRole);
        if (update) {
            return Result.ok("角色修改成功");
        }
        return Result.fail("角色修改失败");
    }


    @PostMapping("/delete")
    @ApiOperation("批量删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "要删除的角色集合")
    })
    @PreAuthorize("hasAuthority('sys_role_delete')")
    public Result delete(@RequestBody String[] ids) {
        if (ids == null || ids.length == 0) {
            return Result.fail("要删除的角色不能为空");
        }
        boolean delete = sysRoleService.removeByIds(Arrays.asList(ids));
        if (delete) {
            return Result.ok("删除成功");
        }
        return Result.fail("删除失败");
    }

}
